package com.evil.kingdom.pojo.entity.action;

import com.evil.kingdom.pojo.dto.person.general.GeneralDto;
import com.evil.kingdom.pojo.entity.base.Side;
import com.evil.kingdom.pojo.entity.base.SideName;
import com.evil.common.mongo.entity.SingleName;
import com.evil.kingdom.pojo.entity.tactics.Tactics;
import com.evil.kingdom.pojo.enums.BattlefieldTypeEnum;
import com.evil.kingdom.task.timer.BattleTimerTask;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 战场
 *
 * @author liyang
 * @date 2024-06-11 14:32
 */
@Slf4j
@Getter
@Setter
public class Battlefield implements SingleName, SideName, Serializable {
    public static final Logger timerTaskLogger = LoggerFactory.getLogger("KINGDOM_TIMER_TASK_LOGGER");
    public static final Logger battlefieldLogger = LoggerFactory.getLogger("KINGDOM_BATTLEFIELD_LOGGER");

    @Serial
    private static final long serialVersionUID = 1L;
    private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    /**
     * 名称
     * (唯一)
     */
    @NotBlank(message = "战场名称不能为空")
    private String name;

    /**
     * 进攻主帅
     */
    @NotBlank(message = "进攻主帅不能为空")
    private String offensiveName;

    /**
     * 防守主帅
     */
    @NotBlank(message = "防守主帅不能为空")
    private String defensiveName;

    /**
     * 地名
     */
    @NotBlank(message = "地名不能为空")
    private String toponym;

    /**
     * 进攻武将列表
     */
    private Set<GeneralDto> offensiveGenerals;

    /**
     * 防守武将列表
     */
    private Set<GeneralDto> defensiveGenerals;

    /**
     * 时间
     */
    private Integer time;

    /**
     * 当前进攻武将
     */
    private GeneralDto offensiveGeneral;

    /**
     * 防守武将
     */
    private GeneralDto defensiveGeneral;

    /**
     * 当前进攻战术
     */
    private Tactics offensiveTactics;

    /**
     * 当前进攻战术
     */
    private Tactics defensiveTactics;

    private List<BattleTimerTask> battleTimerTasks;

    /**
     * 战场类型
     */
    @NotNull(message = "战场类型不能为空")
    private final BattlefieldTypeEnum battlefieldTypeEnum;

    public Battlefield() {
        this.battlefieldTypeEnum = null;
    }

    public Battlefield(String name, Side side, BattlefieldTypeEnum battlefieldTypeEnum, String toponym) {
        this.name = name;
        this.offensiveName = side.getOffensiveName();
        this.defensiveName = side.getDefensiveName();
        this.offensiveGenerals = side.getOffensiveGenerals();
        this.defensiveGenerals = side.getDefensiveGenerals();
        this.battlefieldTypeEnum = battlefieldTypeEnum;
        this.toponym = toponym;
    }

    @Override
    public String getEntityName() {
        return "战场";
    }

    public String printInfo() {
        return String.format("name:%s, offensiveName:%s, defensiveName:%s, type:%s, toponym:%s",
                this.name, this.offensiveName, this.defensiveName, this.battlefieldTypeEnum.name(), this.toponym);
    }
}
